
/*
	Add alert text to login box if we have a bad username or password
*/
function loginFail() {
	$("#divLoginAlert").html('<span class="alert">Bad username or password.</span>');
}	
/*
	Login via callbacks.php
*/
function postLogin(user, pass) {
		$.post("callbacks.php", 
			  { action: "doLogin", user: user, pass: pass },
			  function(data) {
				if(data == "SUCCESS") {
					window.location.reload();
				}
				else {
					loginFail();	
				}
			  });
}
	
/*
	After document loads, javascript will wait for input
*/
$(document).ready(function () {
	//Login button 
	$("#loginForm").click(function () {
		var user = $("#user").val();
		var pass = $("#pass").val();
	
		postLogin(user, pass);
	});
	
	//Logout button clicked
	$("#logoutLink").click(function () {
		$.post("callbacks.php",
			  { action: "doLogout" },
			  function(data) {
				  if(data == "SUCCESS")
				  	window.location.reload();
			  }
		);
	});
	
	
	
	//Validate username method
	$('input[id=username]').keyup(function() {
		var u = $("input[id=username]").val();
		if (u.length < 3) {
			$("#username_errorTxt").html('<span class="alert">Too Short</span>');
		} else if (u.length > 20) {
			$("#username_errorTxt").html('<span class="alert">Too Long</span>');
		} else {
			$("#username_errorTxt").html('<span class="alert">Checking. . . </span>');	
			
			$.post("callbacks.php",
					 { action: 'verifyUsername',
					 user: u },
					 function(data) {
						if(data == "OK") {
							$("#username_errorTxt").html('<span class="alertGreen">Available</span>');
							//enable register button
							$("input[id=registerBtn]").removeAttr('disabled');

						} else {
							$("#username_errorTxt").html('<span class="alert">Already Taken </span>');	
							//disable the button
							$("input[id=registerBtn]").attr('disabled', 'true');
						}
					 }
			);
			
		}
	});
	
	//Register Method
	$("input[id=registerBtn]").click(function() {
		var pw1 = $("input[id=pass]").val();
		var pw2 = $("input[id=pass2]").val();
		var email = $("input[id=email]").val();
		var user = $("input[id=username]").val();
		
		if(user.length < 3) {
			$("#user_errorTxt").html('<span class="alert">Username is too short</span>');
			
			return;
		}
		
		if(pw1 != pw2) {
			//set error, pws don't match
			$("#pw2_errorTxt").html('<span class="alert">Password\'s don\'t match </span>');
			
			return;
		}
		
		if(pw1.length < 4) {
			//set pw error
			$("#pw1_errorTxt").html('<span class="alert">Password is too short</span>')
			return;
		}
		
		if(pw2.length < 4) {
			$("#pw2_errorTxt").html('<span class="alert">Password is too short</span>')
		}
		
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
		if (!filter.test(email)) {
			//set error, fake email used
			return;
		}
		
		$.post("callbacks.php", 
				 { action: 'doRegistration',
				 user: user,
				 email: email,
				 pass: pw1 },
				 function(data) {
					 if(data == "SUCCESS") {
						//log user in with uname and pass
						postLogin(user, pw1);
					 }
					 else {
						//show error
						$("#registerErrText").html('<span class="alert">Registration failure.</span>');
						//debug message
						$("#content").append(data);
					 }
					 
				 }
		);
						
	});
	
	/*
		//////Save profile function//////	
	*/
	$("input[id=saveProfile]").click(function() { 
		var p = new Object();
		p.name = $("input[id=name]").val();
		p.location = $("input[id=location]").val();
		p.gradyear = $("input[id=gradyear]").val();
		p.deg_u = $("input[id=deg_u]").is(':checked');
		p.deg_g = $("input[id=deg_g]").is(':checked');
		p.deg_d = $("input[id=deg_d]").is(':checked');
		p.clubs = $("input[id=clubs]").val();
		p.interests = $("input[id=interests]").val();
		p.department = $("input[id=department]").val();
		p.concentration = $("input[id=concentration]").val();
		p.website = $("input[id=website]").val();											
		
		p.e_employer = $("input[id=employer]").val();
		p.e_position = $("input[id=emp_position]").val();
		p.e_dept = $("input[id=emp_department]").val();
		p.e_started = $("input[id=emp_started]").val();
		p.e_location = $("input[id=emp_location]").val();
		p.e_website = $("input[id=emp_website]").val();
		
		
		$.post("callbacks.php", { 
				 action: 'saveProfile',
				 data: encodeURIComponent(JSON.stringify(p)) },
				 function(data) {
					 //$("#content").append(data);
				 });
		
															
	});
	
	/*
		Resume File Upload Handler
	*/
	$("input[id=resume]").MultiFile({
											  STRING: {
												  remove:'Remover',
												  selected:'Select: $file',
												  denied:'Invalid file $ext'
											  }
											  });
	$("input[id=resume]").change(function() {
			var valr = $("#resume").val();
			$("#content").append(valr);
	});
			
	/*
		Friend addition confirmation
	*/
	$("span[id^='confirmFriend_']").click(function() {
		var friendId = this.id.substr(this.id.indexOf('_')+1	, this.id.length-1);
		$.post("callbacks.php", {
				 action: 'confirmFriend',
				 friendId: friendId },
				 function(data) {
					if(data == "OK")  {
					 $("#confirmFriend_"+friendId).html("Friended Successfully");
					 $("#confirmFriend_"+friendId).removeClass('linkB');
					 $("#confirmFriend_"+friendId).addClass('alertGreen');
					}
				 });
	});
	
	
	/*
		Add Friend Button
	*/
	$("input[id^='requestFriendship_']").click(function() {
		var friendId = this.id.substr(this.id.indexOf('_')+1 , this.id.length-1);
		$.post("callbacks.php", {
				 action: 'addFriend',
				 friendID: friendId },
				 function(data) {
					 if(data == "OK") {
						 $("#addFriendBtn").html("Request Successful");
					 } else {
						 //error
						 $("#content").append(data);
					 }
				 });															  
	});
	
});